//
//  MultLineRadioGroup.m
//  CarTown
//
//  Created by xionghao on 15/8/10.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MultLineRadioGroup.h"
#import "XHRadiobutton.h"
#define boder 10;
@implementation MultLineRadioGroup

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setSelectedIndex:(NSInteger)selectedIndex{
    _selectedIndex=selectedIndex;
    for (int i=0; i<self.subviews.count; i++) {
        UIButton *btn=self.subviews[i];
        btn.selected=NO;
        if (selectedIndex==i) {
            btn.selected=YES;
        }
    }
    
}


-(void)setTitlesarray:(NSArray *)titlesarray{
    _titlesarray=titlesarray;
    CGFloat y;
    CGFloat height=24;
    CGFloat width=(XHScreenwidth-5*10)/4;
    UIFont *font=[UIFont systemFontOfSize:14];
    
    CGFloat x;
   
    for (int i=0; i<titlesarray.count; i++) {
        XHRadiobutton *button=[[XHRadiobutton alloc] init];
        [button setTitle:titlesarray[i] forState:UIControlStateNormal];
        
        
        button.titleLabel.font=font;
        [button setBackgroundImage:[UIImage imageWithStretchableName:@"colornomal"] forState:UIControlStateNormal];
        button.tag=i;
        [button setTitleColor:UIColorWithRGB(165, 165, 165) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(checked:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageWithStretchableName:@"colorblue"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        x=(width+10)*(i%4)+10;
        y=10+i/4*(height+10);
        button.frame=CGRectMake(x, y, width, height);
        [self addSubview: button];
    }
}
-(void)checked:(XHRadiobutton *)button{
    
    
    for (XHRadiobutton *sbutton in self.subviews) {
        sbutton.selected=NO;
    }
    button.selected=YES;
    _selectedValue=button.titleLabel.text;
    _selectedIndex=button.tag;
    if ([_delegate respondsToSelector:@selector(radioclick)]) {
        [_delegate radioclick];
    }

    NSLog(@"index--%d,value--%@",self.selectedIndex,self.selectedValue);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self becomeFirstResponder];
}

@end
